package fm.dongman.modellib.source;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

import fm.dongman.animefm.contractlib.BaseDataSource;
import fm.dongman.animefm.contractlib.IModel;
import fm.dongman.animefm.contractlib.contract.SheetInfoContract;
import fm.dongman.modellib.RemoteHelper;
import fm.dongman.modellib.StaticArea.StaticUrl;
import fm.dongman.modellib.model.ComicModel;
import fm.dongman.modellib.remote.RemoteOperationHelper;

/**
 * @author alex on 2017/10/28.
 */

public class SheetInfoRepository implements SheetInfoContract.ISheetInfoDataSource {
    private static final int mPageSize = 10; // 每页数据个数
    public static final String TYPE_MANDAN = SheetInfoContract.ISheetInfoPresenter.Companion.getTYPE_MANDAN(); // 漫单
    public static final String TYPE_TOPIC = SheetInfoContract.ISheetInfoPresenter.Companion.getTYPE_TOPIC(); // 专题

    public static final int IS_FOLLOWED = SheetInfoContract.ISheetInfoPresenter.Companion.getIS_FOLLOWED(); // 关注
    public static final int NOT_FOLLOWED = SheetInfoContract.ISheetInfoPresenter.Companion.getNOT_FOLLOWED(); // 取消关注

    private static SheetInfoRepository INSTANCE;

    private String mType = TYPE_MANDAN;
    private String mSheetId;
    private int mCurrentPage = 1;
    private boolean isRemote = true;

    private SheetInfoRepository() {
    }

    public static SheetInfoRepository getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new SheetInfoRepository();
        }
        return INSTANCE;
    }

    @Override
    public void setSheetId(@NotNull String type, @NotNull String id, int page) {
        mType = type;
        mSheetId = id;
        mCurrentPage = page;
    }

    @Override
    public void collectSheet(@NotNull String type, @NotNull String sheetId, int follow, @NotNull LoadSourceCallback<? super String> callback) {
        if (!isRemote) {
            callback.onDataLoadFiled();
            return;
        }
        if (type.equals(TYPE_MANDAN)) {
            followSheet(sheetId, follow, callback);
            return;
        }
        collectTopic(sheetId, follow, callback);
    }

    /**
     * 关注漫单
     */
    private void followSheet(@NotNull String sheetId, int follow, @NotNull LoadSourceCallback<? super String> callback) {
        HashMap<String, String> map = new HashMap<>();
        map.put("session_key", StaticUrl.TOKEN_KEY);
        map.put("follow", "" + follow);
        map.put("mandan_id", sheetId);
        JSONObject jsonObject = RemoteHelper.Companion.getInstance().parsePostUrl(StaticUrl.FRANK_MANDAN_INFO, map);
        if (jsonObject == null) {
            callback.onDataLoadFiled();
            return;
        }
        callback.onDataLoaded(null);
    }

    /**
     * 收藏专题
     */
    private void collectTopic(@NotNull String sheetId, int follow, @NotNull LoadSourceCallback<? super String> callback) {
        HashMap<String, String> map = new HashMap<>();
        map.put("session_key", StaticUrl.TOKEN_KEY);
        map.put("follow", "" + follow);
        map.put("topic_id", sheetId);
        JSONObject jsonObject = RemoteHelper.Companion.getInstance().parsePostUrl(StaticUrl.FRANK_TOPIC_INFO, map);
        if (jsonObject == null) {
            callback.onDataLoadFiled();
            return;
        }
        callback.onDataLoaded(null);
    }

    @Override
    public void getData(@NotNull LoadSourceCallback<? super IModel.ISheetModel> callback) {
        if (isRemote) {
            JSONObject object;
            if (StaticUrl.TOKEN_KEY.equals("") && StaticUrl.TOKEN_KEY == null) {
                object = RemoteHelper.Companion.getInstance().parseGetUrl(StaticUrl.SheeInfo + "id=" + mSheetId + "&type=" + mType + "&page=" + mCurrentPage + "&size=" + mPageSize);
            } else {
                object = RemoteHelper.Companion.getInstance().parseGetUrl(StaticUrl.SheeInfo + "id=" + mSheetId + "&type=" + mType + "&page=" + mCurrentPage + "&size=" + mPageSize + "&session_key=" + StaticUrl.TOKEN_KEY);
            }
            if (object == null) {
                callback.onDataLoadFiled();
                return;
            }
            callback.onDataLoaded(RemoteOperationHelper.getOperationHelper().downSheetDetails(object));
        } else callback.onDataLoadFiled();
    }

    @Override
    public void deleteComics(String sheetId, List<? extends IModel.IComicModel> comics, LoadSourceCallback<? super String> loadSourceCallback) {
        if (isRemote) {
            ComicModel model = (ComicModel) comics;
            HashMap<String, String> map = new HashMap<>();
            map.put("x-token", StaticUrl.TOKEN_KEY);
            map.put("mandan_id", sheetId);
            map.put("subject_id", model.getId());
            JSONObject jsonObject = RemoteHelper.Companion.getInstance().parsePostUrl("http://api.dongman.fm/mandan/deleteCollectedItem", map);
            if (jsonObject != null) {
                try {
                    if (jsonObject.getInt("code") == 200) {
                        loadSourceCallback.onDataLoaded("");
                    } else {
                        loadSourceCallback.onDataLoadFiled();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else loadSourceCallback.onDataLoadFiled();
        } else loadSourceCallback.onDataLoadFiled();
    }

    @Override
    public void saveData(IModel.ISheetModel dataModel) {
    }

    @Override
    public void refreshData() {
    }

    @Override
    public void offLine() {
        isRemote = !isRemote;
    }
}
